package org.dtrd.modules.shop.controller;


import org.dtrd.modules.shop.entity.bean.*;
import org.dtrd.modules.shop.entity.po.DtrdEntShcRdProduct;
import org.dtrd.modules.shop.entity.po.DtrdEntShcRdProductPush;
import org.dtrd.modules.shop.entity.request.ProductPushRequest;
import org.dtrd.modules.shop.entity.request.ProductUpdateRequest;
import org.dtrd.modules.shop.service.IDtrdEntShcRdProductPushService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.shop.service.IDtrdEntShcRdProductService;
import org.dtrd.common.api.vo.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;
import org.dtrd.base.BaseController;

import com.baomidou.mybatisplus.core.metadata.IPage;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author sun
 * @since 2022-07-23
 */
@RestController
@RequestMapping("/shop/product")
public class DtrdEntShcRdProductController extends BaseController {

    @Autowired
    private IDtrdEntShcRdProductService productService;

    @Autowired
    private IDtrdEntShcRdProductPushService productPushService;

    @ApiOperation(value = "分页查询列表", response = ProductListInfo.class)
    @PostMapping("/page")
    @AutoLog(value="查询列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getProductList(@RequestBody @Validated ProductPageRequest request){
        handlePageRequest(request);
        IPage<ProductListInfo> page = productService.pageProduct(request);
        return Result.OK(page);
    }

    @ApiOperation("新增或修改")
    @PostMapping("/save")
    @AutoLog(value="新增或修改", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateProduct(@RequestBody ProductDetail dto){
        boolean result = productService.saveOrUpdateProduct(dto);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete")
    @AutoLog(value="删除", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeProduct(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = productService.removeProduct(id);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("推荐")
    @PutMapping("/update")
    @AutoLog(value="推荐", operateType = OPERATE_TYPE_UPDATE)
    public Result<?> updateProduct(@RequestBody ProductUpdateRequest updateRequest){
        if(isIntParamInvalid(updateRequest.getId())) {
            return error(STATE_PARAMS_MISSING);
        }

        DtrdEntShcRdProduct productDetail = productService.getById(updateRequest.getId());
        if (productDetail == null) {
            return error(STATE_PARAMS_ERROR);
        }

        productDetail.setProductIsHot(updateRequest.getStatus());
        productDetail.setProductPushTime(new Date());

        boolean result = productService.updateById(productDetail);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }


    @ApiOperation("商品推送")
    @PostMapping("/push")
    @AutoLog(value="推送", operateType = OPERATE_TYPE_UPDATE)
    public Result<?> pushProductToPatient(@RequestBody @Validated ProductPushRequest pushRequest){

        boolean result = productService.pushProductToPatient(pushRequest);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("最近一次商品推送")
    @GetMapping("/push/last")
    @AutoLog(value="最近一次商品推送", operateType = OPERATE_TYPE_QUERY)
    public Result<?> pushProductToPatient(@RequestParam Integer patientId){

        if (patientId == null) {
            error(STATE_PARAMS_MISSING);
        }

        ProductPushDetail result = productPushService.getLastPushProduct(patientId);
        return Result.OK(result);
    }
}
